//
//  HSYUserModel.m
//  HSY
//
//  Created by 陈浩 on 2017/4/5.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYUserModel.h"

static HSYUserModel *currentUser = nil;

@implementation HSYUserModel

+ (instancetype)sharedCurrentUser {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        currentUser = [[HSYUserModel alloc] init];;
    });
    
    return currentUser;
}

+(id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        currentUser = [super allocWithZone:zone];
    });
    
    return currentUser;
}
#if !__has_feature(objc_arc)

-(id)mutableCopyWithZone:(NSZone *)zone
{
    return currentUser;
}

-(id)copyWithZone:(NSZone *)zone
{
    return currentUser;
}

#endif

#pragma mark -AutoCoding 自动进行归档

static inline NSString * UserselfSaveFile(){
    return [NSHomeDirectory() stringByAppendingString:@"/Documents/HSYUserself"];
}

+ (void)restoreMyself {
    NSString *path = UserselfSaveFile();
    // 从path路径找到并解档案
    [self objectWithContentsOfFile:path];
    [HSYUserModel sharedCurrentUser];
}
+ (void)saveMyselfToDocuments{
    NSString *path = UserselfSaveFile();
    HSYUserModel *userModel = [HSYUserModel sharedCurrentUser];
    [userModel writeToFile:path atomically:YES];
}

+ (void)removeSaveSelf{
    //移除文件
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isHave = [fileManager fileExistsAtPath:UserselfSaveFile()];
    if (isHave) {
        [fileManager removeItemAtPath:UserselfSaveFile() error:nil];
    }
    
    //属性置空
    [HSYUserModel sharedCurrentUser].uid = nil;
    [HSYUserModel sharedCurrentUser].cellphone = nil;
    [HSYUserModel sharedCurrentUser].status = nil;
    [HSYUserModel sharedCurrentUser].idcardf = nil;
    [HSYUserModel sharedCurrentUser].idcardh = nil;
    [HSYUserModel sharedCurrentUser].createTime = nil;
    [HSYUserModel sharedCurrentUser].updateTime = nil;
    //    [GCUserModel sharedCurrentUser].dueSettleAmount = nil;
    //    [GCUserModel sharedCurrentUser].totalAmount = nil;
    //    [GCUserModel sharedCurrentUser].availableAmount = nil;
    [HSYUserModel sharedCurrentUser].accountID = nil;
    [HSYUserModel sharedCurrentUser].globalID = nil;
    [HSYUserModel sharedCurrentUser].dealerID = nil;
    [HSYUserModel sharedCurrentUser].productID = nil;
    [HSYUserModel sharedCurrentUser].weixinRate = 0;
    [HSYUserModel sharedCurrentUser].alipayRate = 0;
    [HSYUserModel sharedCurrentUser].realname = nil;
}


#pragma mark - MJExtension -
+(NSDictionary *)replacedKeyFromPropertyName{
    return @{@"uid" : @"id",@"availableAmount":@"available"};
}


@end
